<?php
namespace Tyrone\Command;
use Tyrone\Lib\BaseRedisPool;
use Tyrone\Lib\DatabasePool;
use Tyrone\Lib\SafeMedoo;

class Base extends BaseRedisPool
{
    protected $redisPool; // ✅ 改为持有连接池
    protected $sDate;
    protected $eDate;
    protected $minus;
    protected $second;
    protected $prefix = 'redis_com_lock_';
    protected $game_mysql;
    protected $mysql;
    protected $old_mysql;
    protected $database;
    protected $platform;
    protected $plat_server;
    protected $config;
    protected $conf;
    protected $params;
    protected $redis;
    protected $channel;

    // ✅ 构造函数接收连接池
    public function __construct($redisPool,$params=[])
    {
        $this->minus = date('H:i');
        $this->redisPool = $redisPool;
        $this->redis = $params['redis']??null;
        $this->sDate = $params['sDate'] ?? ($this->minus=='00:00' ? (date('Y-m-d',strtotime("-1 day"))) : date('Y-m-d'));
        $this->eDate = $params['eDate'] ?? ($this->minus=='00:00' ? (date('Y-m-d',strtotime("-1 day"))) : date('Y-m-d'));
        $this->params = $params['params'] ?? [];
        $this->database = $params['database'] ?? '';
        $this->game_mysql = $params['game_mysql']??null;
        $this->mysql = $params['mysql']??null;
        $this->old_mysql = $params['old_mysql'] ?? '';
        $this->platform = $params['platform']??'';
        $this->plat_server = $params['plat_server']??'';
        $this->config = $params['config']??[];
        $this->conf = $params['conf']??[];
        $this->channel = $params['channel']??null;
    }

    public function mergeData($datas ,callable $action=null){
        foreach ($datas as $key => &$item){
            !isset($item['platform']) && $item['platform'] = $this->platform;
            !isset($item['plat_server']) && $item['plat_server'] = $this->plat_server;
            if(!$action) continue;
            foreach($action($item) as $k =>$value){
                $item[$k] = $value;
            }
        }unset($item);
        return $datas;
    }
    protected function setFrequency($second){
        $this->second = $second;
    }

    protected function result($result){
        return json_encode($result);
    }






    public  function divideCenter()
    {
        ini_set('memory_limit',-1);
        $mysql = $this->mysql;
        $platforms = $mysql->select('s_game_db_config',['platform'],[
            'GROUP' => ['platform']
        ]);
        $wg = new \Swoole\Coroutine\WaitGroup();
        $results = [];
        foreach ($platforms as $platform){
            $wg->add();
            \Swoole\Coroutine::create(function () use ($platform,$wg,&$results) {
                $result = $error = '';
                $mysql = null;
                try{
                    $mysql = DatabasePool::createStaticConnection($this->conf['log_center']);//重新mysql
                    $result = $this->divideHandle($mysql,$platform['platform']);
                }catch (\Throwable $exception){
                    $error = $exception->getMessage().':'.$exception->getFile().'line=='.$exception->getLine();
                } finally {
                    $results[$platform['platform']] = compact('error','result');
                    $wg->done();
                    if ($mysql) {
                        $mysql->pdo = null;
                        unset($mysql);
                    }
                }
            });
        }
        $wg->wait();

        return $this->result($results);
    }
}